<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\model\Version as UM;
use app\admin\validate\VersionValidate;

class Web extends Common
{
    private $noCheckAction = [];//不需要验证的方法

    public function initialize(){
        parent::initialize();
        $this->doCheckAuth($this->noCheckAction);
    }

   public function  index(){
        if(request()->isPost()){
            $data=input('post.');
            $re=db('company')->where('id',1)->update($data);
            if($re)die(outputSuccess([],'编辑成功'));
            die(outputError([],'0'));
        }
        $row=db('company')->where('id',1)->find();
        $this->assign('row',$row);
        return view();
   }

   public function  subject()
   {
        $list=db('pc_cate')->order('create_time desc')->paginate(10);

       $this->assign([
           'list'=>$list,
           'page' => $list->render(),
           'count' => count($list),
       ]);
       return view('kc');
   }


   public  function  add()
   {
       if(request()->isPost()){
           $data=input('post.');
           $data['create_time']=time();
           $re=db('pc_cate')->strict(false)->insert($data);
           if($re)die(outputSuccess([],'编辑成功'));
           die(outputError([],'0'));
       }
       return view();
   }

   public function  del()
   {
       if ($this->request->isAjax()) {
           $id = (int)$this->request->post('id');
           $ret=db('pc_cate')->where('id',$id)->delete();
           if (!$ret) {
               die(outputError([],0));
           }

           die(outputSuccess([], '删除成功'));
       } else {
           die(outputError('请求方式错误'));
       }
   }

   public function  edit()
   {
       if($this->request->isAjax()){
           $request = $this->request->post();
//           var_dump($request);die;
            $ret=db('pc_cate')->where('id',$request['id'])->update($request);
           if(!$ret){
               die(outputError([],'0'));
           }
           die(outputSuccess([], '编辑成功'));
       }else{
           $request = $this->request->get();
           $id = (int)$request['id'];

           $data = db('pc_cate')->where('id',$id)->find();
           $this->assign([
               'data'=>$data,
           ]);
           return $this->fetch();
       }
   }

   public function  xueke()
   {
       $list=db('pc_xueke p')
           ->leftJoin('pc_cate c','c.id=p.cate_id','left')
           ->field('p.*,c.cate')
           ->order('p.create_time desc')->paginate(10);

       $this->assign([
           'list'=>$list,
           'page' => $list->render(),
           'count' => count($list),
       ]);
       return view();
   }

   public function  add_kc()
   {
       if(request()->isPost()){
           $data=input('');
           $data['create_time']=time();
           foreach ($data['teacher'] as $k=>$v){
               $data['teacher_name'][]=db('lecturer')->where('id',$v)->value('title');
           }
           $data['teacher']=join(',',$data['teacher']);
           $data['teacher_name']=join(',',$data['teacher_name']);
           $ret=db('pc_xueke')->insert($data);
           if(!$ret){
               die(outputError([],'0'));
           }
           die(outputSuccess([], '编辑成功'));
       }
       $teacher=db('lecturer')->order('sort asc')->field('id,title')->select();
       $types=db('pc_cate')->order('create_time desc')->select();
       $this->assign('types',$types);
       $this->assign('teacher',$teacher);
       return view();
   }


     public function  edit_kc()
     {
         if($this->request->isAjax()){
             $request = $this->request->post();
             foreach ($request['teacher'] as $k=>$v){
                 $request['teacher_name'][]=db('lecturer')->where('id',$v)->value('title');
             }
             $request['teacher_name']=join(',',$request['teacher_name']);
             $request['teacher']=join(',',$request['teacher']);
             $ret=db('pc_xueke')->where('id',$request['id'])->update($request);
             if(!$ret){
                 die(outputError([],'0'));
             }
             die(outputSuccess([], '编辑成功'));
         }else{
             $request = $this->request->get();
             $id = (int)$request['id'];
             $data = db('pc_xueke')->where('id',$id)->find();
             $teacher=db('lecturer')->order('sort asc')->field('id,title')->select();
              $types=db('pc_cate')->order('create_time desc')->select();
              $this->assign('types',$types);
             $this->assign([
                 'data'=>$data,
                 'lecturer'=>$teacher
             ]);
             return $this->fetch();
         }
         return view();
     }

   public function  del_kc()
   {
       if ($this->request->isAjax()) {
           $id = (int)$this->request->post('id');
           $ret=db('pc_xueke')->where('id',$id)->delete();
           if (!$ret) {
               die(outputError([],0));
           }

           die(outputSuccess([], '删除成功'));
       } else {
           die(outputError('请求方式错误'));
       }
   }

   public function  video_list()
   {

        $id=input('get.id');
       $list=db('pc_video')->where('xueke_id',$id)->order('sort asc')->paginate(10);
       $this->assign([
           'list'=>$list,
           'page' => $list->render(),
           'count' => count($list),
       ]);
       return view();
   }


   public function  add_video()
   {
       if(request()->isPost()){
           $param=input('post.');
           $param['create_time']=time();
           foreach ($param['teacher'] as $k=>$v){
               $param['teacher_name'][]=db('lecturer')->where('id',$v)->value('title');
           }
           $param['teacher_name']=join(',',$param['teacher_name']);
           $param['teacher']=join(',',$param['teacher']);
           $ret=db('pc_video')->strict(false)->insert($param);
             if (!$ret) {
                 die(outputError([],0));
             }

           die(outputSuccess([], '添加成功'));
       }
       $teacher=db('lecturer')->order('sort asc')->field('id,title')->select();
       $data=db('pc_xueke')->field('id,title')->select();
       $this->assign('types',$data);
       $this->assign('teacher',$teacher);
       return view();
   }

   public function  del_video()
   {
       if ($this->request->isAjax()) {
           $id = (int)$this->request->post('id');
           $ret=db('pc_video')->where('id',$id)->delete();
           if (!$ret) {
               die(outputError([],0));
           }

           die(outputSuccess([], '删除成功'));
       } else {
           die(outputError('请求方式错误'));
       }
   }


   public function  edit_video()
   {
       if($this->request->isAjax()){
           $request = $this->request->post();
//           var_dump($request);die;
           foreach ($request['teacher'] as $k=>$v){
               $request['teacher_name'][]=db('lecturer')->where('id',$v)->value('title');
           }
           $request['teacher_name']=join(',',$request['teacher_name']);
           $request['teacher']=join(',',$request['teacher']);
           $ret=db('pc_video')->where('id',$request['id'])->update($request);
           if(!$ret){
               die(outputError([],'0'));
           }
           die(outputSuccess([], '编辑成功'));
       }else{
           $request = $this->request->get();
           $id = (int)$request['id'];
           $teacher=db('lecturer')->order('sort asc')->field('id,title')->select();
           $data = db('pc_video')->where('id',$id)->find();
           $this->assign([
               'data'=>$data,
               'lecturer'=>$teacher
           ]);
           return $this->fetch();
       }
   }

   //新闻动态
     public function  news()
     {
         $list=db('pc_news')->order('create_time desc')->paginate(10);

         $this->assign([
             'list'=>$list,
             'page' => $list->render(),
             'count' => count($list),
         ]);
         return view();
     }

     public function  add_news()
     {

         if(request()->isPost()){
             $data=input('post.');
             $data['create_time']=time();

             $re=db('pc_news')->strict(false)->insert($data);
             if($re)die(outputSuccess([],'编辑成功'));
             die(outputError([],'0'));
         }
         return view();
     }

    public function edit_news()
    {

        if(request()->isPost()){
            $param=input('post.');
            $re=db('pc_news')->where('id',$param['id'])->strict(false)->update($param);
            if (!$re) {
                die(outputError([],0));
            }

            die(outputSuccess([], '操作成功'));
        }
        $id=input('get.id/d');
         $row=db('pc_news')->where('id',$id)->find();
         $this->assign('row',$row);
        return view();
    }

    public function  del_news()
    {
        if ($this->request->isAjax()) {
            $id = (int)$this->request->post('id');
            $ret=db('pc_news')->where('id',$id)->delete();
            if (!$ret) {
                die(outputError([],0));
            }

            die(outputSuccess([], '删除成功'));
        } else {
            die(outputError('请求方式错误'));
        }
    }
    //banner图
    public function ad_list()
    {
        $list=db('pc_banner')->order('create_time desc')->paginate(10);

        $this->assign([
            'list'=>$list,
            'page' => $list->render(),
            'count' => count($list),
        ]);
        return view();
    }

    public function  set_status()
    {
        if(request()->isPost()){
            $param=request()->param();
            if($param['ff']==1){
                $data['status']=0;
            }elseif($param['ff']==-1){
                $data['status']=1;
            }
            $res=db('pc_banner')->where('id',$param['id'])->update($data);
            if($res) die(outputSuccess([], '修改成功'));
            die(outputError('修改失败'));
        }
    }

    public function  del_ad()
    {
        $id=request()->param('id');
        $res=db('pc_banner')->where(['id'=>$id])->delete();
        if($res)die(outputSuccess([], '删除成功'));
        die(outputError('删除失败'));
    }

    public function  add_ad()
    {
        if(request()->isPost()){
            $data['url']=input('post.url');
            $data['create_time']=time();
            $res=db('pc_banner')->insert($data);
            if($res)die(outputSuccess([], '添加成功'));
            die(outputError('添加失败'));
        }
        return view();
    }


    public function  edit_ad()
    {
        if(request()->isPost()){
            $param=request()->param();
            $res=db('pc_banner')->where('id',$param['id'])->setField('url',$param['url']);
            if($res!==false)die(outputSuccess([], '修改成功'));
            die(outputError('修改失败'));
        }

        $id=input('get.id');
        $row=db('pc_banner')->where('id',$id)->find();
        $this->assign('data',$row);
        return view();
    }


}
